$(() => {
    //  倒计时
    let showtime = function (date) {
        let nowtime = new Date(),  //获取当前时间
            endtime = new Date(date);  //定义结束时间
        let lefttime = endtime.getTime() - nowtime.getTime(),  //距离结束时间的毫秒数
            leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)),  //计算天数
            lefth = Math.floor(lefttime / (1000 * 60 * 60) % 24),  //计算小时数
            leftm = Math.floor(lefttime / (1000 * 60) % 60),  //计算分钟数
            lefts = Math.floor(lefttime / 1000 % 60);  //计算秒数
        return leftd + "天" + lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
    };
    setInterval(function () {
        document.getElementById("nextTime").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_cc").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_dl").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_sjz").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_bt").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_qhh").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_nt").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_gz").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_nn").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_hlbe").innerText = showtime('2021/9/1');
        document.getElementById("centerNextTime_dh").innerText = showtime('2021/9/1');
    }, 1000);  //反复执行函数本身


    //  轮播图
    let num = 1;
    setInterval(function () {
        num++;
        if (num === 5) {
            num = 1;
        }
        document.getElementById("showProduct").src = "images/showProduct/" + num + ".png";
        document.getElementById("centerShowProduct").src = "images/showProduct/" + num + ".png";
    }, 2000);

    //  时间
    function getTime() {
        let vWeek, vWeek_s, vDay;
        vWeek = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        let date = new Date();
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        hours = date.getHours();
        minutes = date.getMinutes();
        seconds = date.getSeconds();
        vWeek_s = date.getDay();
        // document.getElementById('centerTime').innerHTML = year + "年" + month + "月" + day + "日" + "\t" + hours + ":" + minutes + ":" + seconds + "\t" + vWeek[vWeek_s];
    }

    setInterval(getTime, 1000);

    function getTime1() {
        let vWeek, vWeek_s, vDay;
        vWeek = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        let date = new Date();
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        hours = date.getHours();
        minutes = date.getMinutes();
        seconds = date.getSeconds();
        vWeek_s = date.getDay();
        document.getElementById('time').innerHTML = year + "年" + month + "月" + day + "日" + "\t" + hours + ":" + minutes + ":" + seconds + "\t" + vWeek[vWeek_s];

    }

    setInterval(getTime1, 1000);

    function getWeather(city, cityID) {
        let result;
        let high;
        let low;
        let type;
        let fengxiang;
        let xmlhttp;
        if (window.XMLHttpRequest) {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp = new XMLHttpRequest();
        } else {
            // IE6, IE5 浏览器执行代码
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //建立连接
        xmlhttp.open("GET", "http://wthrcdn.etouch.cn/weather_mini?city=" + city, true);
        //发送请求
        xmlhttp.send();
        //响应
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                let obj = JSON.parse(xmlhttp.responseText);     //  将json数据转换成对象
                let smg = obj.data.forecast;
                high = smg[3].high;
                low = smg[3].low;
                type = smg[3].type;
                fengxiang = smg[3].fengxiang;
                // let a = high.split(" ")[1];
                // result = type + " " + high + " " + low + " " + fengxiang;
                result = low.split(" ")[1] + " ~ " + high.split(" ")[1] + " " + fengxiang + " " + type;
                document.getElementById(cityID).innerText = result;
            }
        };
    }

    getWeather('长春', 'nextWeather');
    getWeather('长春', 'centerNextWeather_cc');
    getWeather('大连', 'centerNextWeather_dl');
    getWeather('石家庄', 'centerNextWeather_sjz');
    getWeather('包头', 'centerNextWeather_bt');
    getWeather('刚察', 'centerNextWeather_qhh');
    getWeather('南通', 'centerNextWeather_nt');
    getWeather('广州', 'centerNextWeather_gz');
    getWeather('南宁', 'centerNextWeather_nn');
    getWeather('呼伦贝尔', 'centerNextWeather_hlbe');
    getWeather('敦煌', 'centerNextWeather_dh');

    //  查看详情控制
    $('.centerButton').on('click', function () {
        document.getElementById('cesiumContainer').style.display='block';
        document.getElementById('centerWeather').style.display='none';
        document.getElementById('centerBar').style.display='none';
        document.getElementById('centerForecast').style.display='none';
        document.getElementById('centerImg').style.display='none';
    });

    $('#leftDown').on('click',function () {
        document.getElementById('cesiumContainer').style.display='none';
        document.getElementById('centerWeather').style.display='none';
        document.getElementById('centerBar').style.display='none';
        document.getElementById('centerForecast').style.display='block';
        document.getElementById('centerImg').style.display='none';
    });

    $('#rightDown').on('click',function () {
        document.getElementById('cesiumContainer').style.display='none';
        document.getElementById('centerWeather').style.display='none';
        document.getElementById('centerBar').style.display='none';
        document.getElementById('centerForecast').style.display='none';
        document.getElementById('centerImg').style.display='block';
    });
});